//
//  VisitViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/28.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "VisitViewController.h"
#import "VisitHeaderTableViewCell.h"
#import "VisitBodyTableViewCell.h"
#import "postListModel.h"
#import "OneFeedViewController.h"
#import "XLPhotoBrowser.h"

@interface VisitViewController ()<UITableViewDelegate, UITableViewDataSource, XLPhotoBrowserDelegate>

@property(nonatomic, strong)UITableView *tableview;
@property(nonatomic, strong)NSMutableArray *postListMutArray;

@end

@implementation VisitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"胖虎";
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.llz_width, self.view.llz_height) style:UITableViewStyleGrouped];
    _tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    _tableview.sectionFooterHeight = 1;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:_tableview];
    [_tableview registerNib:[UINib nibWithNibName:@"VisitHeaderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"VisitHeaderTableViewCell"];
    [_tableview registerNib:[UINib nibWithNibName:@"VisitBodyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"VisitBodyTableViewCell"];
    
    _postListMutArray = [[NSMutableArray alloc]init];
    _postListMutArray = [self addTestData];
}

#pragma mark - *******tableviewDelegate dataSource*******

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *identify = @"VisitHeaderTableViewCell";
    static NSString *identifyB = @"VisitBodyTableViewCell";
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify forIndexPath:indexPath];
        return cell;
    }
   VisitBodyTableViewCell *cellB = [tableView dequeueReusableCellWithIdentifier:identifyB forIndexPath:indexPath];
    [cellB setModel:_postListMutArray[indexPath.row]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photosClicked:)];
    cellB.topImageView.userInteractionEnabled = YES;
    [cellB.topImageView addGestureRecognizer:tap];
    return cellB;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return _postListMutArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 220;
    }else{
        return 120;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    if (indexPath.section == 0)return;
    OneFeedViewController *one = [[OneFeedViewController alloc]init];
    [self.navigationController pushViewController:one animated:YES];
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return @"2018年";
    }
    return @"";
}
#pragma mark - *******添加测试数据*******
- (NSMutableArray *)addTestData{
    NSMutableArray *Data = [[NSMutableArray alloc]init];
    NSArray *photoArr = @[@[@"风景13",@"风景14",@"风景15",@"风景16"],@[@"军事1",@"军事2",@"军事3",@"军事4",@"军事5"]];
    NSArray *postArr = @[@"西湖的音乐喷泉很不错哦！",@"我是一个军事迷"];
    NSArray *timeArr = @[@"今天",@"3月21日"];
    for (int i = 1; i < 3; i++) {
        NSDictionary *dic = @{@"photos":photoArr[i - 1],
                              @"postContent":postArr[i - 1],
                              @"location":@"杭州市",
                              @"time":timeArr[i - 1],
                              @"moneyNum":@"231",
                              @"thumbNum":@"321",
                              @"commentNum":@"123",
                              };
        postListModel *model = [[postListModel alloc]initWithPostListModel:dic];
        [Data addObject:model];
    }
    return Data;
}

#pragma mark - *******私有方法*******
///预览图片
- (void)photosClicked:(UIPanGestureRecognizer *)pan{
    NSMutableArray <UIImage *>* images = [[NSMutableArray alloc]init];
    postListModel *model = _postListMutArray[0];
    for (NSString *name in model.photos){
        UIImage *image = [UIImage imageNamed:name];
        [images addObject:image];
    }
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:images currentImageIndex:0];
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:@"取消" deleteButtonTitle:nil otherButtonTitles:@"保存图片",nil];
    
}
@end
